﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Meals
{
    public class UserMeal:lIUserMeals
    {
        public List<Model.Meal> GetUserMeals(int userId, ref Model.NutriLeafDBEntities context)
        {
            List<Model.Meal> meal = new List<Model.Meal>();

          List<Model.UserMeal> me= context.UserMeals.Where(p => p.UserID == userId).ToList();
          foreach (Model.UserMeal i in me) {
              meal.Add(i.Meal);
          }
          return meal;
        }

        public void AddUserMeal(int userId, int mealId, ref Model.NutriLeafDBEntities context)
        {
            Model.UserMeal meal = new Model.UserMeal();
            meal.UserID = userId;
            meal.MealID = mealId;
            context.UserMeals.AddObject(meal);
            context.SaveChanges();
        }

        public void RemoveMealFromUser(int userId, int mealId, ref Model.NutriLeafDBEntities context)
        {
             context.DeleteObject(context.UserMeals.Where(p => p.UserID == userId && p.MealID == mealId));
            context.SaveChanges();

        }

        public void RemoveMeal(int userMealId, ref Model.NutriLeafDBEntities context)
        {
           context.DeleteObject( context.UserMeals.Where(p => p.UserMealID == userMealId).First());
        }
    }
}